import 'dart:async';
import 'package:flame/components.dart';
import 'package:flame/flame.dart';
import 'package:flutter/material.dart';
import '../common/mahjong_context.dart';

// 剩余的牌

class RoomRuleInfo extends PositionComponent {
  var winCard = Flame.images.fromCache('mahjong/commons/card_tips.png');
  RoomRuleInfo()
      : super(
            size: Vector2(MahjongContext().screenWidth / 3,
                MahjongContext().screenHeight / 3),
            position: Vector2(MahjongContext().screenWidth / 2,
                MahjongContext().screenHeight / 2),
            anchor: Anchor.center);

  @override
  FutureOr<void> onLoad() {
    /* MahjongContext.registerRoomFunc<RoomInfoUpdateEvent>((event) {
      _roomInfo = event.roomInfo;
    }); */
  }

  @override
  void render(Canvas canvas) {
    var gameInfo = MahjongContext.roomInfo.gameInfo;
    var gameRule = MahjongContext.roomInfo.rule;
    super.render(canvas);
    if (MahjongContext.roomInfo.status != 1) {
      return;
    }
    final String firstLineText = gameRule?.roomTypeStr ?? "";
    final String secondLineText =
        "房号:${gameInfo.code ?? ""} \n局数:${gameRule?.userCount ?? ""}";
    String thirdLineText = gameRule?.ruleStr ?? "";
    thirdLineText = thirdLineText == "" ? "... ..." : thirdLineText;
    // 获取组件的宽度和高度
    // 获取组件的宽度和高度
    final componentWidth = size.x;
    final componentHeight = size.y;

    // 计算每行文字的高度
    final lineHeight = componentHeight / 5;

    // 绘制背景渐变色
    final Paint gradientPaint = Paint()
      ..shader = LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: [Colors.blue.shade400, Colors.blue.shade800],
      ).createShader(Rect.fromLTWH(0, 0, componentWidth, componentHeight));
    canvas.drawRect(
        Rect.fromLTWH(0, 0, componentWidth, lineHeight), gradientPaint);

    var gradientPaint2 = Paint()
      ..color = Color.fromARGB(255, 220, 242, 248)
      ..style = PaintingStyle.fill;
    canvas.drawRect(
        Rect.fromLTWH(0, lineHeight, componentWidth, lineHeight * 2),
        gradientPaint2);

    // 绘制第一行文本
    final firstLinePainter = TextPainter(
      text: TextSpan(
          text: firstLineText,
          style: TextStyle(
              textBaseline: TextBaseline.ideographic,
              color: Color.fromARGB(255, 255, 255, 255),
              fontSize: lineHeight * 0.6,
              height: 1)),
      textDirection: TextDirection.ltr,
    );
    firstLinePainter.layout();
    final firstLineY = (lineHeight - firstLinePainter.height) / 2; // 计算文本垂直居中位置
    final firstLineX = (componentWidth - firstLinePainter.width) / 2;
    firstLinePainter.paint(canvas, Offset(firstLineX, firstLineY));

    // 绘制第二行文本
    final secondLinePainter = TextPainter(
      text: TextSpan(
          text: secondLineText,
          style: TextStyle(
            color: Color.fromARGB(255, 40, 42, 207),
            fontSize: lineHeight / 2,
          )),
      textDirection: TextDirection.ltr,
    );
    secondLinePainter.layout();

    final secondLineY = lineHeight +
        (lineHeight * 2 - secondLinePainter.height) / 2; // 计算文本垂直居中位置
    final secondLineX = (componentWidth - secondLinePainter.width) / 2;
    secondLinePainter.paint(canvas, Offset(secondLineX, secondLineY));

    var gradientPaint3 = Paint()
      ..color = Color.fromARGB(255, 220, 242, 248)
      ..style = PaintingStyle.fill;
    canvas.drawRect(
        Rect.fromLTWH(0, lineHeight * 3, componentWidth, lineHeight * 2),
        gradientPaint3);

    // 绘制第三行文本
    final thirdLinePainter = TextPainter(
      text: TextSpan(
          text: thirdLineText,
          style: TextStyle(
            color: Color.fromARGB(255, 0, 132, 255),
            fontSize: lineHeight * 0.5,
            height: 1.0,
          )),
      textDirection: TextDirection.ltr,
      ellipsis: "...",
      maxLines: 3, // 设置最大行数
    );
    thirdLinePainter.layout(maxWidth: componentWidth - lineHeight);
    thirdLinePainter.paint(canvas, Offset(lineHeight * 0.5, lineHeight * 3));
  }
}
